<body>
    <table id="dbresource_list" style="width:80%;height:80%">
    </table>
    <div id="dbresource_list_tb">
        <div style="margin: 10px">
            数据库: <select id="selectDB4Resource" class="easyui-combogrid" style="width:250px" />
            表名: <input class="easyui-textbox" data-options="prompt:'数据库表名'" id="tableName"/>
            是否视图: <select id="isViewSelect" class="easyui-combobox">
                        <option value="-1">所有</option>
                        <option value="0">表</option>
                        <option value="1">视图</option>
                    </select>
            <a href="#" id="searchDbResource" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">查询</a>
        </div>
        <div >
            <a href="javascript:syncTable()" id="syncTable" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true">同步数据库</a>
            <a href="javascript:openUpdateDbResourceWindow()" id="updateDeleteFlag" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">更新</a>
        </div>
        <div id="updateDbResourceWindow" class="easyui-window" title="Add Database" data-options="iconCls:'icon-save',modal:true,closed:true" style="width:500px;height:500px;padding:5px;">
            <div class="easyui-layout" data-options="fit:true">
                <div id="add_database_form" data-options="region:'center'" style="padding:10px;">
                    <input type="hidden" id="updateDelFlagResourceId"/>
                    <input type="hidden" id="updateDelFlagResourceIdFlag"/>
                    <select id="resourceDeleteFlag" class="easyui-combobox" name="state" style="width:200px;">
                        <option value="0">未删除</option>
                        <option value="1">已删除</option>
                    </select>
                </div>
                <div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
                    <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" onclick="javascript:doupdateDeleteFlag()" style="width:80px">Ok</a>
                    <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="javascript:$('#updateDbResourceWindow').window('close')" style="width:80px">Cancel</a>
                </div>
            </div>
        </div>

    </div>
    <script>
        $("#selectDB4Resource").combogrid({
            mode: 'remote',
            url: '/admin/database/listnopage',
            idField:'id',
            textField:'dbName',
            columns:[[
                {field:'id',title:'ID', checkbox: true},
                {field:'dbName',title:'数据库名称'},
                {field:'url',title:'url'},
                {field:'userName',title:'用户名'},
                {field:'dbType',title:'类型'},
                {field:'createUserName',title:'创建人'},
                {field:'createTime',title:'创建时间'},
                {field:'updateUserName',title:'更新人'},
                {field:'updateTime',title:'更新时间'}
            ]]
        });


        $('#dbresource_list').datagrid({
            title: '表/视图列表',
            url:'/admin/dbresource/list',
            columns:[[
                {field:'id',title:'ID', checkbox: true},
                {field:'dbName',title:'数据库名称'},
                {field:'dbType',title:'类型'},
                {field:'tableName',title:'表名'},
                {field:'isViewStr',title:'是否视图'},
                {field:'delStatus',title:'删除状态'},
                {field:'createTime',title:'创建时间'}
            ]],
            fit: true,
            idField: 'id',
            pagination:true,
            rownumbers:true,
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: true,
            toolbar: '#dbresource_list_tb'
        });

        var p_dbresource_list = $('#dbresource_list').datagrid('getPager');
        $(p_dbresource_list).pagination({
            pageSize: 10,//每页显示的记录条数，默认为10
            pageList: [10,20,50,100],//可以设置每页记录条数的列表
            beforePageText: '第',//页数文本框前显示的汉字
            afterPageText: '页    共 {pages} 页',
            displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
        });

        $("#searchDbResource").click(function(){
            var param = {
                name: $("#tableName").val(),
                dbId: $("#selectDB4Resource").combogrid("getValue")
            };

            var select = $("#isViewSelect").val();
            if (select != -1) {
                param.isView = select;
            }

            $('#dbresource_list').datagrid("load", param);
        });

        function syncTable() {
            var dbid = $("#selectDB4Resource").combogrid("getValue");
            if (!dbid) {
                $.messager.alert("Error","请选择数据库!",'warn');
                return;
            }
            post("/admin/dbresource/sync/" + dbid, {}, function(data) {
                if (data.code == 200) {
                    $.messager.show({
                        title: '同步成功',
                        msg: '同步成功！',
                        showType: 'show'
                    });
                    $("#dbresource_list").datagrid("reload");
                } else {
                    $.messager.alert('同步失败!',data.message,'error');
                }
            })
        }

        function openUpdateDbResourceWindow() {
            var record = $("#dbresource_list").datagrid("getSelected");
            if (!record) {
                $.messager.alert("Error","请选择一条记录!",'warn');
                return;
            }

            $("#updateDelFlagResourceId").val(record.id);
            var del = record.delete;
            if (del == 1)
                del = 0;
            else if (del == 0)
                del = 1;
            $("#updateDelFlagResourceIdFlag").val(del);
            $('#updateDbResourceWindow').window('open');
        }

        function doupdateDeleteFlag() {
            var id = $("#updateDelFlagResourceId").val();
            var del = $("#updateDelFlagResourceIdFlag").val();
            post("/admin/dbresource/del/" + id + "/" + del, {}, function(data) {
                if (data.code == 200) {
                    $.messager.show({
                        title: '更新成功',
                        msg: '更新成功！',
                        showType: 'show'
                    });
                    $("#dbresource_list").datagrid("reload");
                } else {
                    $.messager.alert('删除失败!',data.message,'error');
                }
                $('#updateDbResourceWindow').window('close');
            });
        }



    </script>
</body>